﻿Public Class FormUpdate
    Private animatedImage As System.Drawing.Bitmap
    Private m_cnt As Integer = 0

    Private Sub FormUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        Dim result As Boolean = False
        Dim ResultListViewItem As ListViewItem
        UpdateButton.Enabled = False
        EndButton.Enabled = False

        Try
            ResultListViewItem = AddListLine("ﾌｧｲﾙﾀﾞｳﾝﾛｰﾄﾞ")
            result = fileDownload(tmpFileName)
            If result Then
                ResultListViewItem.SubItems(2).Text = "完"
                Dim fileName As String = CApplicationManager.GetRealPath(tmpFileName, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                Dim ZipTmpPath As String = CApplicationManager.GetRealPath(tmpPath, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                Dim ProgramFilesPath As String = CApplicationManager.GetRealPath("", CApplicationManager.EnumSpecialFolder.PATH_APPLICATION)
                If System.IO.Directory.Exists(ZipTmpPath) Then
                    System.IO.Directory.Delete(ZipTmpPath, True)
                End If
                ResultListViewItem = AddListLine("ﾌｧｲﾙ展開")
                If unZipFile(fileName, ZipTmpPath) = "OK" Then
                    ResultListViewItem.SubItems(2).Text = "完"

                    Dim root() As String = System.IO.Directory.GetDirectories(ZipTmpPath)
                    If root.Length = 1 Then
                        Dim i As Integer
                        Dim topDirList() As String = System.IO.Directory.GetDirectories(root(0))
                        Dim topFileList() As String = System.IO.Directory.GetFiles(root(0))

                        Dim ErrorFlg As Boolean = False

                        For i = 0 To topFileList.Length - 1
                            Dim file As String = topFileList(i)
                            Dim destFileName As String = System.IO.Path.Combine(ProgramFilesPath, System.IO.Path.GetFileName(file))

                            ResultListViewItem = AddListLine(System.IO.Path.GetFileName(file))
                            If file.IndexOf("ICSharpCode.SharpZipLib.dll") = -1 And file.IndexOf("Update.exe") = -1 Then
                                Try
                                    Debug.Write(file & " -> " & destFileName & " .....")

                                    CFileLog.WriteLog("File_COPY-" & file & " -> " & destFileName & " .....", CFileLog.LEVEL_NORMAL, CFileLog.EnumLogType.DOWNLOAD)

                                    System.IO.File.Copy(file, destFileName, True)
                                    Debug.WriteLine("OK")
                                    ResultListViewItem.SubItems(2).Text = "完"
                                Catch ex As Exception
                                    Debug.WriteLine("ERR")
                                    ErrorFlg = True
                                    ResultListViewItem.SubItems(2).Text = "ER"
                                End Try
                            Else
                                Debug.Write(file & " -> " & destFileName & " .....")
                                Debug.WriteLine("対象外")
                                ResultListViewItem.SubItems(2).Text = "-"
                            End If

                        Next
                        Dim path1 As String = topDirList(0)

                        'Dim destPathName1 As String = System.IO.Path.Combine(ProgramFilesPath, p1)

                        'Try
                        '    Dim p1 As String = ProgramFilesPath.Replace("\update\Release", "")
                        '    CopyDirectory(ZipTmpPath, p1, True)
                        '    ResultListViewItem.SubItems(2).Text = "完"
                        'Catch ex As Exception
                        '    ResultListViewItem.SubItems(2).Text = "ER"
                        'End Try


                        'For i = 0 To topDirList.Length - 1
                        '    Dim path As String = topDirList(i)
                        '    Dim p As String = path.Replace(root(0) & "\", "")
                        '    Dim destPathName As String = System.IO.Path.Combine(ProgramFilesPath, p)

                        '    MessageBox.Show("path(" & i & "):" & path)
                        '    MessageBox.Show("p(" & i & "):" & p)
                        '    MessageBox.Show("destPathName(" & i & "):" & destPathName)

                        '    ResultListViewItem = AddListLine(System.IO.Path.GetFileName(p))

                        '    Try
                        '        Debug.Write(path & " -> " & destPathName & " .....")
                        '        CopyDirectory(path, destPathName, True)
                        '        Debug.WriteLine("OK")
                        '        ResultListViewItem.SubItems(2).Text = "完"
                        '    Catch ex As Exception
                        '        Debug.WriteLine("ERR")
                        '        ErrorFlg = True
                        '        ResultListViewItem.SubItems(2).Text = "ER"
                        '    End Try
                        'Next

                        If Not ErrorFlg Then
                            ResultListViewItem = AddListLine("正常終了")
                        Else
                            ResultListViewItem = AddListLine("異常終了")
                            UpdateButton.Enabled = True
                        End If
                    End If
                Else
                    ResultListViewItem = AddListLine("異常終了")
                    UpdateButton.Enabled = True
                End If
            Else
                ResultListViewItem = AddListLine("異常終了")
                UpdateButton.Enabled = True
            End If
            MsgBox("更新が完了しました。")
        Catch ex As Exception
            ResultListViewItem = AddListLine("異常終了")
            UpdateButton.Enabled = True
        End Try


        EndButton.Enabled = True
        EndButton.Focus()
    End Sub

    Private Function AddListLine(ByVal msg As String) As ListViewItem
        Dim itm As New ListViewItem
        Dim tgt As New ListViewItem.ListViewSubItem
        Dim res As New ListViewItem.ListViewSubItem

        itm.SubItems.Add(tgt)
        itm.SubItems.Add(res)

        m_cnt += 1
        itm.Text = CStr(m_cnt)
        tgt.Text = msg

        ResultListView.BeginUpdate()
        ResultListView.Items.Add(itm)
        ResultListView.EndUpdate()

        Return itm
    End Function

    Private Sub EndButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndButton.Click
        Dim app As CApplicationManager = CApplicationManager.GetInstance()


        app.ExecuteUpdateProcess("LeaderEye.exe")
        Me.Close()
    End Sub
End Class